package com.sztx.worker_monitor.web.handle;

import java.util.List;

import com.sztx.worker_monitor.common.StringUtils;
public class CronAnalysis {
	static private String cronErrDes="错误的cron表达式"; 
	
	
	static public String getCronDescription(String cron){
		
		
		if(cron == null){
			return cronErrDes;
		}
		
		String des = "";
		
	//将表达式解析为  不同的时间单位表达
	List<String> cronParts =  StringUtils.splitString(cron, " ") ;
	if(cronParts.size() != 6 && cronParts.size() != 7){
		return cronErrDes;
	}
	System.out.println("cron = "+cron);
	System.out.println("秒  ="+cronParts.get(0)+",分="+
							cronParts.get(1)+",小时="+
							cronParts.get(2)+",日="+
							cronParts.get(3)+",月="+
							cronParts.get(4)+",星期="+
							cronParts.get(5));
	try{
		if(cronParts.size() == 7){
			System.out.println(",年 ="+cronParts.get(6));
			if(!"*".equals(cronParts.get(6)))
				des += yearAnalysis(cronParts.get(6));
		}
		if(!"*".equals(cronParts.get(4)))
			des += monthAnalysis(cronParts.get(4));
		
		des += weekAnalysis(cronParts.get(5));
		if(!("*".equals(cronParts.get(3))&&"*".equals(cronParts.get(2))))
			des += dayAnalysis(cronParts.get(3));
		if(!("*".equals(cronParts.get(2)) && "*".equals(cronParts.get(1))))
			des += hourAnalysis(cronParts.get(2));
		if(!("*".equals(cronParts.get(1))&& "*".equals(cronParts.get(0))))
			des += mimuteAnalysis(cronParts.get(1));
		des += secAnalysis(cronParts.get(0));

		return des;
		
	}catch(Exception e){
		e.printStackTrace();
		return e.toString();
		
	}
}
	//秒部分的 分析
	static private String  secAnalysis(String secCron) throws Exception {
		
		if(secCron==null) throw new Exception(cronErrDes); 
		String secDescription = "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(secCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "第 0秒开始触发";	
				}
				else{
					secDescription += "第"+innerParts.get(0)+"秒开始触发";
				}
				secDescription += "每隔"+cronParts.get(1)+"秒，触发一次";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription += "第"+innerParts.get(0)+"到第"+innerParts.get(1)+"秒";
			secDescription += "每隔"+cronParts.get(1)+"秒，触发一次";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 *
		if(secCron.equals("*")) return "每秒钟触发一次";
		
		//包含 "-"
		cronParts =  StringUtils.splitString(secCron, "-") ;
		if( cronParts.size() == 2){
			secDescription +="第"+ cronParts.get(0)+"秒到第" +
					cronParts.get(1)+"秒，每秒均触发一次";
			return secDescription;
		}
		
		//包含","
		cronParts =  StringUtils.splitString(secCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			secDescription +="第";
			secDescription += cronParts.get(i);
			secDescription+="秒 ";
		}
		if(cronParts.size()>=2)
			secDescription +="均触发一次";
		else
			secDescription +="触发一次";
		return secDescription;	
	}

	
	//分钟 部分的 分析
	static private String  mimuteAnalysis(String mimuteCron) throws Exception {
		
		if(mimuteCron==null) throw new Exception(cronErrDes); 
		String secDescription = "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(mimuteCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "第 0分钟开始";	
				}
				else{
					secDescription += "第"+innerParts.get(0)+"分钟开始";
				}
				secDescription += "每隔"+cronParts.get(1)+"分钟";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription += "第"+innerParts.get(0)+"到第"+innerParts.get(1)+"分钟";
			secDescription += "每隔"+cronParts.get(1)+"分钟";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 *
		if(mimuteCron.equals("*")) return "每分钟";
		
		//包含 "-"
		cronParts =  StringUtils.splitString(mimuteCron, "-") ;
		if( cronParts.size() == 2){
			secDescription +="第"+ cronParts.get(0)+"分钟到第" +
					cronParts.get(1)+"分钟";
			return secDescription;
		}
				
		//包含","
		cronParts =  StringUtils.splitString(mimuteCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			secDescription +="第";
			secDescription += cronParts.get(i);
			secDescription+="分钟 ";
		}
		return secDescription;	
	}

	
	//小时 部分的 分析
	static private String  hourAnalysis(String hourCron) throws Exception {
		
		if(hourCron==null) throw new Exception(cronErrDes); 
		String secDescription = "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(hourCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "第 0小时开始";	
				}
				else{
					secDescription += "第"+innerParts.get(0)+"小时开始";
				}
				secDescription += "每隔"+cronParts.get(1)+"小时 ";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription += "第"+innerParts.get(0)+"到第"+innerParts.get(1)+"小时";
			secDescription += "每隔"+cronParts.get(1)+"小时";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 *
		if(hourCron.equals("*")) return "每小时";
		
		//包含 "-"
		cronParts =  StringUtils.splitString(hourCron, "-") ;
		if( cronParts.size() == 2){
			secDescription +="第"+ cronParts.get(0)+"小时到第" +
					cronParts.get(1)+"小时";
			return secDescription;
		}
				
		//包含","
		cronParts =  StringUtils.splitString(hourCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			secDescription +="第";
			secDescription += cronParts.get(i);
			secDescription+="小时";
		}
		secDescription +=" ";
		return secDescription;	
	}
	
	
	//日 部分的 分析
	static private String  dayAnalysis(String dayCron) throws Exception {
		
		if(dayCron==null) throw new Exception(cronErrDes); 
		//包含 ?
		if(dayCron.equals("?")) return "";
		
		String secDescription = "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(dayCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "第 0日开始";	
				}
				else{
					secDescription += "第"+innerParts.get(0)+"日开始";
				}
				secDescription += "每隔"+cronParts.get(1)+"日 ";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription += "第"+innerParts.get(0)+"到第"+innerParts.get(1)+"日";
			secDescription += "每隔"+cronParts.get(1)+"日";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 *
		if(dayCron.equals("*")) return "每天";
		
		//包含 "-"
		cronParts =  StringUtils.splitString(dayCron, "-") ;
		if( cronParts.size() == 2){
			secDescription +="第"+ cronParts.get(0)+"小时到第" +
					cronParts.get(1)+"小时 ";
			return secDescription;
			
		}
		
		//包含","
		cronParts =  StringUtils.splitString(dayCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			secDescription +="第";
			secDescription += cronParts.get(i);
			secDescription+="日";
		}
		secDescription +=" ";
		return secDescription;	
	}
	
	
	//月 部分的 分析
	static private String  monthAnalysis(String monthCron) throws Exception {
		
		if(monthCron==null) throw new Exception(cronErrDes); 
		String secDescription = "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(monthCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "第 0月开始";	
				}
				else{
					secDescription += "第"+innerParts.get(0)+"月开始";
				}
				secDescription += "每隔"+cronParts.get(1)+"月";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription += "第"+innerParts.get(0)+"到第"+innerParts.get(1)+"月";
			secDescription += "每隔"+cronParts.get(1)+"月 ";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 "-"
		cronParts =  StringUtils.splitString(monthCron, "-") ;
		if( cronParts.size() == 2){
			secDescription +="第"+ cronParts.get(0)+"月到第" +
					cronParts.get(1)+"月  ";
			return secDescription;
			
		}
		
		//包含 *
		if(monthCron.equals("*")) return "每月";
		
		//包含","
		cronParts =  StringUtils.splitString(monthCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			secDescription +="第";
			secDescription += cronParts.get(i);
			secDescription+="月";
		}
		secDescription +=" ";
		return secDescription;	
	}
	
	
	//年  部分的 分析
	static private String  weekAnalysis(String weekCron) throws Exception {
		
		if(weekCron==null) throw new Exception(cronErrDes); 
		String secDescription = "";
		
		if(weekCron.equals("?")) return "";
		
		//包含"/"
		List<String> cronParts =  StringUtils.splitString(weekCron, "/") ;
		if(cronParts.size() == 2){
			
			List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
			if(innerParts.size() == 1){
				if(innerParts.get(0).equals("*")){
					secDescription += "星期天开始";	
				}
				else{
					secDescription += getWeekNo(innerParts.get(0))+"开始";
				}
				secDescription += "每隔"+cronParts.get(1)+"天";
				return secDescription;
				
			}
				
			else if(innerParts.size() != 2)
				throw new Exception(cronErrDes);
			secDescription +=  getWeekNo(innerParts.get(0))+"到"+ getWeekNo(innerParts.get(1));
			secDescription += "每隔"+cronParts.get(1)+"天 ";
			return secDescription;
			
		}	
		else if(cronParts.size() != 1){
			throw new Exception(cronErrDes);
		}
		
		//包含 *
		if(weekCron.equals("*")) return "每星期";
		
		//包含 "-"
		cronParts =  StringUtils.splitString(weekCron, "-") ;
		if( cronParts.size() == 2){
			secDescription += getWeekNo(cronParts.get(0))+"到"+
					getWeekNo(cronParts.get(1))+" ";
			return secDescription;
			
		}
				
		
		//包含 #
		cronParts =  StringUtils.splitString(weekCron, "#");
		if(cronParts.size() == 2){
			secDescription += "本月第"+cronParts.get(1)+"周的"+getWeekNo(cronParts.get(0));
			return secDescription;	
		}
		//包含L

		if((weekCron.indexOf("L")) != -1){
			secDescription += "本月最后一个"+getWeekNo(weekCron.substring(1));
			return  secDescription;
		}
		
		//包含","
		cronParts =  StringUtils.splitString(weekCron, ",") ;

		for(int i =0; i < cronParts.size(); i++){
			
			secDescription += getWeekNo(cronParts.get(i));
			secDescription +=" ";
		}
		secDescription +=" ";
		return secDescription;	
	}
	
	static private String  getWeekNo(String no)  {
		if(no == null) return "";
		if(no.equals("1") ||no.equals("SUN")){
			return "星期天";
		}
		else if(no.equals("2") ||no.equals("MON")){
			return "星期一";
		}
		else if(no.equals("3") ||no.equals("TUE")){
			return "星期二";
		}
		else if(no.equals("4") ||no.equals("WED")){
			return "星期三";
		}
		else if(no.equals("5") ||no.equals("THU")){
			return "星期三";
		}
		else if(no.equals("6") ||no.equals("FRI")){
			return "星期五";
		}
		else if(no.equals("7") ||no.equals("SAT")){
			return "星期六";
		}
		
		return "";
		
	}
	
	
	//月 部分的 分析
	static private String  yearAnalysis(String yearCron) throws Exception {
			
			if(yearCron==null)  return ""; 
			String secDescription = "";
			
			//包含"/"
			List<String> cronParts =  StringUtils.splitString(yearCron, "/") ;
			if(cronParts.size() == 2){
				
				List<String> innerParts =  StringUtils.splitString(cronParts.get(0), "-") ;
				if(innerParts.size() == 1){
					if(innerParts.get(0).equals("*")){
						secDescription += "第 19710年开始";	
					}
					else{
						secDescription += innerParts.get(0)+"年开始";
					}
					secDescription += "每隔"+cronParts.get(1)+"年";
					return secDescription;
					
				}
					
				else if(innerParts.size() != 2)
					throw new Exception(cronErrDes);
				secDescription += innerParts.get(0)+"到"+innerParts.get(1)+"年";
				secDescription += "每隔"+cronParts.get(1)+"年";
				return secDescription;
				
			}	
			else if(cronParts.size() != 1){
				throw new Exception(cronErrDes);
			}
			
			//包含 *
			if(yearCron.equals("*")) return "每年";
			
			//包含 "-"
			cronParts =  StringUtils.splitString(yearCron, "-") ;
			if( cronParts.size() == 2){
				secDescription += cronParts.get(0)+"年到"+
						cronParts.get(1)+"年  ";
				return secDescription;
				
			}
			
			//包含","
			cronParts =  StringUtils.splitString(yearCron, ",") ;

			for(int i =0; i < cronParts.size(); i++){
				secDescription += cronParts.get(i);
				secDescription+="年 ";
			}
			secDescription +=" ";
			return secDescription;	
		}
		
	
}
